<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import modal from 'utils/modal';
    import api from 'api/api';
    import navigator from 'utils/navigator';
    import notification from 'utils/notification';
    import route from 'constants/route';
    import constant from 'constants/constant';
    import cartService from 'services/cartService';

    /*渲染购物车数据*/
    function vendData(vm,data){
        vm.cartList = vm.cartList.slice(0,0).concat(data.cart || []); //购物车列表(利用vue的长列表数据优化渲染逻辑)
        vm.totalNum = data.totalNum; //购物车总数量
        vm.totalCheckedNum = data.totalCheckedNum; //购物车勾选商品总数量
        vm.totalCheckedMoney = data.totalCheckedMoney; //购物车勾选商品总金额

        //是否全选
        if(vm.totalCheckedNum == vm.totalNum){
            vm.checkedAll = true;
        } else {
            vm.checkedAll = false;
        }
    }

    export default{
        data(){
            return{
                pageLoaded: false, //是否页面加载完成
                isEditShow: false, //是否显示购物车编辑
                checkedAll: true, //是否全部勾选
                swipeRef: null,
                cartList: [], //购物车列表
                totalNum: 0, //购物车商品总数量
                totalCheckedNum: 0, //购物车勾选商品总数量
                totalCheckedMoney: 0 //购物车勾选商品总金额
            }
        },
        created(){
            let self = this;
            self.get();
        },
        methods: {
            /**获取购物车*/
            get(){
                let self = this;
                api.sendHttp("getCartApi",{}).then(res=>{
                    self.pageLoaded = true;
                    vendData(self,res.data);
                    //初始化购物车时同步购物车角标和底部导航数据
                    self.setCartStorage(res.data);
                    notification.send(constant.EVENT_CART_DELETE_FOR_FOOTER,res.data);
                },res=>{
                    self.pageLoaded = true;
                    modal.toast(res.msg || "获取购物车失败");
                });
            },
            /**勾选购物车*/
            checkCart(str,callback){
                let self = this;
                api.sendHttp('checkCartApi',{
                    goods: str
                }).then(res => {
                    let data = res.data || {};
                    vendData(self,data);

                    self.setCartStorage(data);

                    if(typeof callback == 'function') callback(data);
                }, res => {
                    modal.toast(res.msg);
                });
            },
            editCartClick(){ //编辑购物车开关
                this.isEditShow = !this.isEditShow;
                this.onswipeStart();
            },
            checkGoods(str){ //勾选当前单个商品
                this.checkCart(str,data=>{
                    //发送勾选购物车商品事件 更新底部导航
                    notification.send(constant.EVENT_CART_CHECKED,data);
                });
            },
            checkShop(str){ //勾选店铺全部商品
                this.checkCart(str,data=>{
                    //发送勾选购物车商品事件 更新底部导航
                    notification.send(constant.EVENT_CART_CHECKED,data);
                });
            },
            checkAll(){ //全选购物车所有商品
                let self = this;
                let tmp = [];
                for(let s in this.cartList){
                    let shop = this.cartList[s];
                    for(let g in shop.goods){
                        let goods = shop.goods[g];
                        let str = goods.id + '_' + goods.num + '_';

                        if(!this.checkedAll) {
                            str = str + 1;
                        } else {
                            str = str + 0
                        }
                        tmp.push(str);
                    }
                }

                this.checkCart(tmp.join('='),data=>{
                    //发送勾选购物车商品事件 更新底部导航
                    notification.send(constant.EVENT_CART_CHECKED,data);
                });
            },
            delGoods(data){ //删除当前单个商品
                vendData(this,data);

                this.setCartStorage(data);

                //发送购物车删除底部导航更新通知
                notification.send(constant.EVENT_CART_DELETE_FOR_FOOTER,data);
            },
            delEdit(){ //删除购物车编辑后的勾选商品
                let self = this;
                if(!self.totalCheckedNum) {
                    modal.toast('至少勾选一项');
                    return false;
                }
                modal.confirm({
                    message:  self.checkedAll ? '删除全部商品?' :'删除选中商品?'
                }).then(()=>{
                    let temp = [];
                    for(let s in self.cartList){
                        let shop = self.cartList[s];
                        for(let g in shop.goods){
                            let goods = shop.goods[g];
                            if(goods.g_checked){
                                temp.push(goods.id);
                            }
                        }
                    }
                    let id = temp.join('_');
                    api.sendHttp('delCartApi', {
                        id: id
                    }).then(res => {
                        vendData(self,res.data);

                        self.setCartStorage(res.data);

                        //发送购物车商品删除事件
                        notification.send(constant.EVENT_CART_DELETE,temp);

                        //发送购物车删除底部导航更新通知
                        notification.send(constant.EVENT_CART_DELETE_FOR_FOOTER,res.data);
                    }, res => {
                        modal.toast(res.msg || '删除失败');
                    });
                });
            },
            updateGoods(data){ //更新当前单个商品
                vendData(this,data);
            },
            setCartStorage(data){ //set storage购物车数据
                data = data || {};
                let _cartData = {
                    totalNum: data.totalNum || 0, //购物车数量
                    totalItems: data.totalItems || 0, //购物车种类
                    totalMoney: data.totalMoney || 0, //购物车总价格
                    totalCheckedNum: data.totalCheckedNum || 0, //购物车选中商品的总数量
                    totalCheckedMoney: data.totalCheckedMoney || 0 //购物车选中商品的总价格
                };
                cartService.storageCartInfo(_cartData);
            },
            goToConfirm(){ //去确认订单付款
                if(!this.totalCheckedNum) return false;
                let tmp = [];
                for(let s in this.cartList){
                    let shop = this.cartList[s];
                    for(let g in shop.goods){
                        let goods = shop.goods[g];
                        if(goods.g_checked == 1){
                           let str = goods.id + '_' + goods.num;
                           tmp.push(str);
                        }
                    }
                }

                navigator.pop();

                let options = {
                    url: route.confirm,
                    title: '确认订单',
                    params: {
                        goods: tmp.join('='),
                        from: 'fromCart'
                    }
                };
                navigator.push(options);
            },
            onswipeStart(){
                if(this.swipeRef) {
                    let _ref = this.$refs[this.swipeRef][0];
                    if(_ref) {
                        _ref.resetSwipe();
                    }
                }
            },
            onswipeEnd(refId){
                this.swipeRef = refId || null;
            }
        }
    }
</script>
